# demo03_gen.py
import random
# 列表生成式
nums = [random.randint(1, 100) for i in range(5)]
# list01 = []
# for i in range(5):
#     list01.append(random.randint(1, 100))
print(nums)
# 生成器表达式
nums2 = (random.randint(1, 100) for i in range(5))
print(type(nums2))  # <class 'generator'>
print(nums2)  # <generator object <genexpr> at 0x7f31240d27d8>
for item in nums2:  # 通过for循环可以获取生成器中的数据
    print(item)
# >>> sum([i for i in range(1000000000)])
# 已杀死
# >>> sum((i for i in range(1000000000)))
# 499999999500000000






# print(nums2.__next__())  # 6

# 61
# 36
# 17
# 84
# 88

print(nums2.__next__())
print(nums2.__next__())
print(nums2.__next__())
print(nums2.__next__())
print(nums2.__next__())